﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using FileManagerGlobal_Library;

namespace FileManager.ZipApplication
{
    public partial class ZipApplication : DevExpress.XtraEditors.XtraForm
    {
        BindingSource bs;
        BindingList<FileAndFolderAddress> list;
        public ZipApplication()
        {
            InitializeComponent();
            LookAndFeel.SetSkinStyle("Wizard97");
            StartPosition = FormStartPosition.CenterScreen;
            bs = new BindingSource();
            list = new BindingList<FileAndFolderAddress>();
            bs.DataSource = list;
            Gc_FileOrFolderAddress.DataSource = bs;
        }

        private void Tsmi_DeleteChoose_Click(object sender, EventArgs e)
        {
            foreach (var selectedRow in gridView1.GetSelectedRows())
            {
                var chooseObject =
                    gridView1.GetRow(selectedRow) as FileAndFolderAddress;
                list.Remove(chooseObject);
            }
            //Gc_FileOrFolderAddress.DataSource = _fileAndFolderAddresses;
        }

        private void Btn_ChooseFile_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog { Multiselect = true, RestoreDirectory = true }; //创建一个OpenFileDialog  
            if (dlg.ShowDialog() != DialogResult.OK) return;
            foreach (var fileName in dlg.FileNames)
            {
                if (list.Any(x => fileName.Equals(x.Address))) continue;
                list.Add(new FileAndFolderAddress
                                                {
                                                    Address = fileName,
                                                    IsFolder = false
                                                });
            }
            //Gc_FileOrFolderAddress.DataSource = _fileAndFolderAddresses;
        }

        private void Btn_ChooseFolder_Click(object sender, EventArgs e)
        {
            var dialog = new FolderBrowserDialog { Description = "请选择文件保存路径" };
            if (dialog.ShowDialog() != DialogResult.OK) return;
            var foldPath = dialog.SelectedPath;
            if (list.Any(x => foldPath.Equals(x.Address))) return;
            list.Add(new FileAndFolderAddress
            {
                Address = foldPath,
                IsFolder = true
            });
            //Gc_FileOrFolderAddress.DataSource = _fileAndFolderAddresses;
        }

        private void Cms_GcMenum_Opening(object sender, CancelEventArgs e)
        {
            if (gridView1.GetSelectedRows().Length <= 0) e.Cancel = true;
        }

        private WaitForm1 waitForm1;
        private void Btn_ZipApplication_Click(object sender, EventArgs e)
        {
            var zipBackgroundWorker = new BackgroundWorker();
            var dialog = new FolderBrowserDialog { Description = "请选择压缩目录" };
            if (dialog.ShowDialog() != DialogResult.OK) return;
            var foldPath = dialog.SelectedPath;
            var dialog2 = new FolderBrowserDialog { Description = "请选择保存目录" };
            if (dialog2.ShowDialog() != DialogResult.OK) return;
            var foldPath2 = dialog2.SelectedPath;
            var fileName = Rb_ClientInstallFile.Checked
                               ? GlobalVariable.ClientInstallZipName
                               : GlobalVariable.ClientAppVariable.ClientUpdateFileName;
            foldPath2 = Path.Combine(foldPath2, fileName);
            var zipClass = new ZipClass
                                    {
                                        ZipDir = foldPath,
                                        ZipFile = foldPath2
                                    };
            zipBackgroundWorker.DoWork += zipBackgroundWorker_DoWork;
            zipBackgroundWorker.RunWorkerAsync(zipClass);
            waitForm1 = new WaitForm1();
            waitForm1.SetCaption("正在压缩");
            waitForm1.SetDescription("正在压缩");
            waitForm1.StartPosition = FormStartPosition.CenterScreen;
            waitForm1.ShowDialog(); 
        }

        void zipBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var args = e.Argument as ZipClass;
            ZipHelper.ZipFileDirectory(args.ZipDir, args.ZipFile);
            waitForm1.Invoke((EventHandler) delegate
                                                {
                                                    waitForm1.Close();
                                                    XtraMessageBox.Show("压缩完成");
                                                });
        }



        private void Btn_UZip_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog { Multiselect = false, RestoreDirectory = true }; //创建一个OpenFileDialog  
            dlg.Filter = "Zip Files(*.data)|*.data|All Files(*.*)|*.*";
            if (dlg.ShowDialog() != DialogResult.OK) return;
             var dialog = new FolderBrowserDialog { Description = "请选择文件解压路径" };
            if (dialog.ShowDialog() != DialogResult.OK) return;
            var fileName = dlg.FileName;
            var unzipBackgroundWorker = new BackgroundWorker();
            unzipBackgroundWorker.DoWork += unzipBackgroundWorker_DoWork;
            var zipClass = new ZipClass
            {
                ZipDir = dialog.SelectedPath,
                ZipFile = fileName
            };
            unzipBackgroundWorker.RunWorkerAsync(zipClass);
            waitForm1 = new WaitForm1();
            waitForm1.SetCaption("正在解压");
            waitForm1.SetDescription("正在解压");
            waitForm1.StartPosition = FormStartPosition.CenterScreen;
            waitForm1.ShowDialog();
        }

        void unzipBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var args = e.Argument as ZipClass;
            ZipHelper.UnZip(args.ZipFile,args.ZipDir, "", true);
            waitForm1.Invoke((EventHandler)delegate
            {
                waitForm1.Close();
                XtraMessageBox.Show("解压完成");
            });
            
        }
    }

    public class FileAndFolderAddress
    {
        public string Address { get; set; }
        public bool IsFolder { get; set; }
    }

    public class ZipClass
    {
        public string ZipDir { get; set; }
        public string ZipFile { get; set; }
    }
}